package com.shujia.day16;

import java.io.*;

/*
    比较4种复制读写的速度
    数据源：E:\bigdata32\32期day16 字节流 1.mp4


 */
public class CopyFileTest2 {
    public static void main(String[] args) {
        //1、普通字节流一次读写一个字节
//        fun1(); // > 10min 2231484
        //3、字节缓冲流一次读写一个字节
//        fun2(); //2834749

        //2、普通字节流一次读写一个字节数组
//        fun3(); // 共消耗 707 毫秒

        //4、字节缓冲流一次读写一个字节数组
        fun4(); // 共消耗 502 毫秒

    }

    public static void fun4(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //创建普通字节输入流对象
            bis = new BufferedInputStream(new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4"));
            //创建普通字节输出流对象那个
            bos = new BufferedOutputStream(new FileOutputStream("D:\\a4.mp4"));

            byte[] bytes = new byte[1024];
            int length = 0;

            long start = System.currentTimeMillis();
            while ((length= bis.read(bytes))!=-1){
                bos.write(bytes,0,length);
                bos.flush();
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void fun3(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建普通字节输入流对象
            fis = new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4");
            //创建普通字节输出流对象那个
            fos = new FileOutputStream("D:\\a3.mp4");

            byte[] bytes = new byte[1024];
            int length = 0;
            long start = System.currentTimeMillis();
            while ((length=fis.read(bytes))!=-1){
                fos.write(bytes,0,length);
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //字节缓冲流一次读写一个字节
    public static void fun2(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //创建普通字节输入流对象
            bis = new BufferedInputStream(new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4"));
            //创建普通字节输出流对象那个
            bos = new BufferedOutputStream(new FileOutputStream("D:\\a2.mp4"));

            int i = 0;

            int i2 = 1;
            long start = System.currentTimeMillis();
            while ((i=bis.read())!=-1){
                System.out.println(i2++);
                bos.write(i);
                bos.flush();
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //普通字节流一次读写一个字节
    public static void fun1() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建普通字节输入流对象
            fis = new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4");
            //创建普通字节输出流对象那个
            fos = new FileOutputStream("D:\\a1.mp4");

            int i = 0;

            int i2 = 1;
            long start = System.currentTimeMillis();
            while ((i=fis.read())!=-1){
                System.out.println(i2++);
                fos.write(i);
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
